package mercurius.fitness.client.util;

import java.util.Vector;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.URL;
import com.google.gwt.http.client.RequestBuilder.Method;

public class HTTPUtil {
	
	private HTTPUtil() {
		
	}
	
	
	public static void doMethod(Method method,String url, String data,Entry[] headers, RequestCallback callback) {
		RequestBuilder builder = new RequestBuilder(method, url);
		try {
			if(headers != null) {
				for(int i=0;i<headers.length;i++) {
					builder.setHeader(headers[i].getName(),headers[i].getValue());
					builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
				}
			}
			else {
				builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
			}
			
			Logger.debug("Executing "+ method+ " URL: "+url+"?"+data);
			Request response = builder.sendRequest(data, callback);
		} catch (RequestException e) {
			Logger.error("Failed to send the request",e);
		}
	}
	
	public static void doPost(String url, Entry[] entries, RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.POST,url,HTTPUtil.buildQueryString(entries),null,callback);
	}
	
	public static void doGet(String url, Entry[] entries, RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.GET,url,HTTPUtil.buildQueryString(entries),null,callback);
	}

	public static void doPost(String url, Object[] entries, RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.POST,url,HTTPUtil.buildQueryString(entries),null,callback);
	}
	
	public static void doGet(String url, Object[] entries, RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.GET,url,HTTPUtil.buildQueryString(entries),null,callback);
	}
	
	public static void doPost(String url, Object[] entries, Entry[] headers,RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.POST,url,HTTPUtil.buildQueryString(entries),headers,callback);
	}
	
	public static void doGet(String url, Object[] entries, Entry[] headers,RequestCallback callback) {
		HTTPUtil.doMethod(RequestBuilder.GET,url,HTTPUtil.buildQueryString(entries),headers,callback);
	}
	
	public static Entry[] Entries(Object[] queryEntries) {
		Entry[] entries = new Entry[queryEntries.length];
		for(int i=0;i<entries.length;i++) {
			entries[i] = (Entry) queryEntries[i];
		}
		return entries;
	}
	
	public static Entry[] Entries(Vector queryEntries) {
		return Entries(queryEntries.toArray());
	}
	
	public static String buildQueryString(Object[] queryEntries) {
		return HTTPUtil.buildQueryString(Entries(queryEntries));
	}
	
	public static String buildQueryString(Vector queryEntries) {
		return HTTPUtil.buildQueryString(Entries(queryEntries));
	}
	
	public static String buildQueryString(Entry[] queryEntries) {
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0, n = queryEntries.length; i < n; ++i) {
			Entry queryEntry = queryEntries[i];
			
			if (i > 0) {
				sb.append("&");
			}
			
			// encode the characters in the name
			String encodedName = URL.encodeComponent(queryEntry.getName());
			sb.append(encodedName);
			
			sb.append("=");
			
			// encode the characters in the value
			String val = "";
			if(!queryEntry.getValue().equals("")) {
			val = URL.encodeComponent(queryEntry.getValue());
			}
			else {
				val = "";
			}
			sb.append(val);
		}
		
		return sb.toString();
	}
	
}
